package initConfig

import sysAuth.*

/**
 * Created by Administrator on 2018-05-04.
 */
class InitAdmin {


    public SysUsers initUser(String userNo,String userName){
        SysUsers userAdmin = SysUsers.findByUserNo(userNo)
        if(!userAdmin){
            userAdmin = SysUsers.newInstance()
            userAdmin.setUserNo(userNo)
            userAdmin.setUserName(userName)
            userAdmin.setPassWord("1".encodeAsMD5())
            userAdmin.setCreator(userNo)
            userAdmin.save(flush: true)
        }



        return userAdmin
    }

    //初始化角色
    public void  initRole(String userNo){
         for(String key : SysInitData.AdminRole.keySet()){
             SysRoles role = SysRoles.findByRoleNo(key)
             if(!role){
                 role = SysRoles.newInstance()
                 role.setRoleNo(key)
                 role.setRoleName(SysInitData.AdminRole.get(key))
                 role.setRoleType(key=="ADMIN_FUNC_ROLE"?"FUNC_ROLE":"DATA_ROLE")
                 role.save(flush: true)
             }

             Long uId = SysUsers.findByUserNo(userNo)?.id
             Long rId = role?.id

             SysUserRoles userRoles = SysUserRoles.findByUserIdAndRoleId(uId,rId)
             if(!userRoles){
                 userRoles = SysUserRoles.newInstance()
                 userRoles.setUserId(uId)
                 userRoles.setRoleId(rId)
                 userRoles.save(flush: true)
             }
         }

        //funcNo:"FUNC_01",funcName:"系统管理",orderNum:0,parentNo:null
        for(Map func:SysInitData.Grade1Function){
            initFunctions(
                    "FUNC_${SysFunctions.count}",
                    func.get("funcName") ,
                    func.get("orderNum"),
                    SysFunctions.findByFuncNo(func.get("parentNo")),
                    func?.get("path"),
                    func?.get("operateNos"))
        }

        for(Map func:SysInitData.Grade2Function){
            initFunctions( "FUNC_${SysFunctions.count}",
                    func.get("funcName") ,
                    func.get("orderNum"),
                    SysFunctions.findByFuncNo(func.get("parentNo")),
                    func?.get("path"),
                    func?.get("operateNos"))
        }
    }


    //配置菜单
    public SysFunctions initFunctions(String funcNo,String funcName,int orderNum,SysFunctions parent,String path,String operateNos){
        SysFunctions func = SysFunctions.findByFuncNo(funcNo)
        if(!func){
            func = SysFunctions.newInstance()
            func.setFuncNo(funcNo)
            func.setFuncName(funcName)
            func.setOrderNum(orderNum)
            func.setParentId(parent?.id)
            func.setPath(path)
            func.setOperateNos(operateNos)
            func.save(flush: true)
        }
        //println func.getMap()
        return func
    }

    //初始化配置
    def initViewCfg(){
        def cfg =SysViewCfg.count()
        if(!cfg){
            cfg = SysViewCfg.newInstance()
            cfg.save(flush: true)
        }
    }

    /**
     * 初始化定义操作权限
     */
    def initOpt(){

    }

}
